package com.example.springbootdemo.Util;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.CookieManager;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.sun.corba.se.impl.orbutil.closure.Constant;
import org.apache.commons.lang.builder.ToStringBuilder;

import java.util.Random;

public class httpclient {

        static WebClient webClient = null;
        static Random random = new Random();

        static {
//1.创建对象
                webClient = new WebClient(BrowserVersion.CHROME);
//2.设置参数
//启动js
                webClient.getOptions().setJavaScriptEnabled(true);
//关闭css渲染
                webClient.getOptions().setCssEnabled(false);
//启动重定向
                webClient.getOptions().setRedirectEnabled(true);
//设置连接超时时间 ，这里是10S。如果为0，则无限期等待
                webClient.getOptions().setTimeout(1000 * 15);
//启动cookie管理
                webClient.setCookieManager(new CookieManager());
//启动ajax代理
                webClient.setAjaxController(new NicelyResynchronizingAjaxController());
//js运行时错误，是否抛出异常
                webClient.getOptions().setThrowExceptionOnScriptError(false);
//设置浏览器请求信息
                webClient.addRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                webClient.addRequestHeader("Accept-Encoding", "gzip, deflate");
                webClient.addRequestHeader("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
                webClient.addRequestHeader("Connection", "keep-alive");
                webClient.addRequestHeader("Upgrade-Insecure-Requests", "1");
        }

        public static void runJs(String url) {
                try {
                        webClient.addRequestHeader("User-Agent",random.toString());
//等待js渲染执行 waitime等待时间(ms)
                        webClient.waitForBackgroundJavaScript(100 * 10);
//3.获取页面
                        webClient.getPage(url);
                } catch (Exception e) {
                        e.printStackTrace();
                } finally {
                        if (webClient != null) {
                                webClient.close();
                        }
                }

        }
        public static void main(String[] args) {
                runJs("http://cdn.hxycdn.com:8081/");
                System.out.println("sss");
        }
}